package org.zeapp.opencloud.framework.synchro;

import java.util.List;

import org.zeapp.opencloud.framework.chunking.Chunking;
import org.zeapp.opencloud.framework.cloud.Cloud;

/**
 * This is the service in charge of scanning aliases, cut files and synchronize
 * them with the cloud
 * 
 * @author Amphiprion
 * 
 */
public class SynchronizationService extends Thread {
	/** The current running service. Null if no service running. */
	public static SynchronizationService runningService;

	private Cloud cloudService;
	private Chunking chunkingService;
	private List<FolderAlias> folderAliases;

	private SynchronizationService(Cloud cloudService,
			Chunking chunkingService, List<FolderAlias> folderAliases) {
		this.chunkingService = chunkingService;
		this.cloudService = cloudService;
		this.folderAliases = folderAliases;
	}

	/**
	 * Return the current running service. Null if no service running.
	 * 
	 * @return the running service or null if the service is not running.
	 */
	public static SynchronizationService getRunningService() {
		return runningService;
	}

	/**
	 * Start a new SynchronizationService process.
	 * 
	 * @param cloudService
	 *            the cloud service
	 * @param chunkingService
	 *            the chunking service
	 * @param folderAliases
	 *            the list of FolderAlias mapping
	 * @return the new running service
	 * @throws SynchronizationException
	 *             if an error occurs
	 */
	public static SynchronizationService startService(Cloud cloudService,
			Chunking chunkingService, List<FolderAlias> folderAliases)
			throws SynchronizationException {
		if (runningService != null) {
			throw new SynchronizationException(
					"A service is already running. Please stop it before running another servive.",
					"ZESYNC00001");
		}
		runningService = new SynchronizationService(cloudService,
				chunkingService, folderAliases);
		runningService.start();
		return runningService;

	}

	@Override
	public void run() {
	}
}
